/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainQtcjmn;
import me.zhengjie.domain.TrainSxcjmn;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainQtcjmnService;
import me.zhengjie.domain.vo.TrainQtcjmnQueryCriteria;
import me.zhengjie.mapper.TrainQtcjmnMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-05
**/
@Service
@RequiredArgsConstructor
public class TrainQtcjmnServiceImpl extends ServiceImpl<TrainQtcjmnMapper, TrainQtcjmn> implements TrainQtcjmnService {

    private final TrainQtcjmnMapper trainQtcjmnMapper;

    public TrainQtcjmn findOne(Long trainId, Long trainItemId) {
        return trainQtcjmnMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainQtcjmn> queryAll(TrainQtcjmnQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainQtcjmnMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainQtcjmn> queryAll(TrainQtcjmnQueryCriteria criteria){
        return trainQtcjmnMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainQtcjmn resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainQtcjmn resources) {
        TrainQtcjmn trainQtcjmn = getById(resources.getId());
        trainQtcjmn.copy(resources);
        saveOrUpdate(trainQtcjmn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainQtcjmn> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainQtcjmn trainQtcjmn : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练 ID", trainQtcjmn.getTrainId());
            map.put("训练项目", trainQtcjmn.getTrainItemId());
            map.put("躯体旋转错误_顺时针90(自我空间感觉|训练持续时间)", trainQtcjmn.getQtSsz());
            map.put("躯体旋转错误_逆时针90（自我空间感觉|训练持续时间)", trainQtcjmn.getQtNsz());
            map.put("科里奥利错觉_仰卧翻转错觉_头右倾(自我空间感觉|训练持续时间)", trainQtcjmn.getQtTyq());
            map.put("科里奥利错觉_仰卧翻转错觉_头左倾(自我空间感觉|训练持续时间)", trainQtcjmn.getQtTzq());
            map.put("科里奥利错觉_倾斜翻转错觉_前低头(自我空间感觉|训练持续时间)", trainQtcjmn.getQtQdt());
            map.put("科里奥利错觉_倾斜翻转错觉_直立(自我空间感觉|训练持续时间)", trainQtcjmn.getQtZl());
            map.put("知识掌握(0：熟练掌握，1：掌握，2：了解：3：不了解)", trainQtcjmn.getZszw());
            map.put("评定结果(0:优秀，1：良好，2：合格，3：不合格：4：复训合格)", trainQtcjmn.getResult());
            map.put("建议", trainQtcjmn.getJy());
            map.put("记录人", trainQtcjmn.getCreateBy());
            map.put("记录日期", trainQtcjmn.getDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}